package com.rds.workflow.service.impl;


import com.rds.common.core.domain.entity.SysUser;
import com.rds.common.utils.DateUtils;
import com.rds.system.mapper.SysUserMapper;
import com.rds.workflow.domain.RdaWfNode;
import com.rds.workflow.mapper.RdaWfNodeMapper;
import com.rds.workflow.service.IRdaWfNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RdaWfNodeServiceImpl implements IRdaWfNodeService {

    @Autowired
    private RdaWfNodeMapper rdaWfNodeMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public int updateRdaWfNode(RdaWfNode rdaWfNode) {
        return rdaWfNodeMapper.updateRdaWfNode(rdaWfNode);
    }

    @Override
    public int updateRdaWfNodeByStateid(Long workflowid, Long stateid, Long nodeType, Date startTime) {
        return rdaWfNodeMapper.updateRdaWfNodeByStateid(workflowid,stateid,nodeType,startTime);
    }

    @Override
    public int updateRdaWfNodeByToStateid(Long workflowid, Long stateid,String responseType) {
        return rdaWfNodeMapper.updateRdaWfNodeByToStateid(workflowid,stateid,responseType);
    }

    @Override
    public List<RdaWfNode> selectRdaWfNodebyUser(Long workflowid) {
        return rdaWfNodeMapper.selectRdaWfNodebyUser(workflowid);
    }

    @Override
    public RdaWfNode selectRdaWfNodebyInfo(Long id) {
        return rdaWfNodeMapper.selectRdaWfNodebyInfo(id);
    }

    @Override
    public RdaWfNode selectRdaWfNodebyById(Long id) {
        return rdaWfNodeMapper.selectRdaWfNodebyById(id);
    }

    @Override
    public List<RdaWfNode> selectRdaWfNodeList(RdaWfNode rdaWfNode) {
        return rdaWfNodeMapper.selectRdaWfNodeList(rdaWfNode);
    }

    @Override
    public List<RdaWfNode> selectRdaWfNodeTemplete(Long workflowid,String operationType) {

        List<RdaWfNode> rdaWfNodeList=rdaWfNodeMapper.selectRdaWfNodeTemplete(workflowid,operationType);
        rdaWfNodeList.forEach((RdaWfNode rdaWfNode) -> {
            String fromid=rdaWfNode.getFromidArray();
            String toid=rdaWfNode.getToidArray();
            String getCostTime=rdaWfNode.getCostTimeDay();
            if(getCostTime!=null && !"".equals(getCostTime)){
                Integer date=Integer.parseInt(getCostTime);
                rdaWfNode.setCostTimeDay(DateUtils.parseTimeSeconds(date,0));
            }
            String fromNameArray = "";
            String toNameArray = "";
            if(fromid!=null){
                List<RdaWfNode> fromNameList=rdaWfNodeMapper.selectRdaWfNodebyInId(fromid);
                for(RdaWfNode rdaWfNode2:fromNameList){
                    fromNameArray+=rdaWfNode2.getNodeName()+",";
                }
                rdaWfNode.setFromName(fromNameArray.substring(0,fromNameArray.lastIndexOf(",")));
            }else{
                rdaWfNode.setFromName("");
            }

            if(toid!=null){
                List<RdaWfNode> toNameist=rdaWfNodeMapper.selectRdaWfNodebyInId(toid);
                for(RdaWfNode rdaWfNode2:toNameist){
                    toNameArray+=rdaWfNode2.getNodeName()+",";
                }
                rdaWfNode.setToName(toNameArray.substring(0,toNameArray.lastIndexOf(",")));
            }else{
                rdaWfNode.setToName("");
            }

            ArrayList<String> userNames=new ArrayList<String>();
            if(null!=rdaWfNode.getUserId() && rdaWfNode.getUserId().length()!=0){
                String[] userIds=rdaWfNode.getUserId().split(",");
                for (int i = 0; i < userIds.length; i++) {
                    String userId=userIds[i];
                    SysUser sysUser=sysUserMapper.selectUserById(Long.valueOf(userId));
                    if(sysUser!=null){
                        userNames.add(sysUser.getUserName());
                    }
                }
            }
            rdaWfNode.setUserId(userNames.toString().substring(1,userNames.toString().length()-1));
        });
        return rdaWfNodeList;
    }

    @Override
    public RdaWfNode selectRdaWfNodeStartOrEnd(Long workflowid, Long nodeType) {
        return rdaWfNodeMapper.selectRdaWfNodeStartOrEnd(workflowid,nodeType);
    }

    @Override
    public int updateRdaWfNodeStateid(Long id, Long stateid) {
        return rdaWfNodeMapper.updateRdaWfNodeStateid(id, stateid);
    }

    @Override
    public int deleteRdaWfNodeByWorkflowid(Long workflowid) {
        return rdaWfNodeMapper.deleteRdaWfNodeByWorkflowid(workflowid);
    }

    @Override
    public int updateRdaWfNodeByStateids(Long workflowid, Long stateid, String stateids) {
        return rdaWfNodeMapper.updateRdaWfNodeByStateids(workflowid,stateid,stateids);
    }

    @Override
    public List<RdaWfNode> selectRdaWfNodeByFromIdList(Long nodeId, String operationType) {
        return rdaWfNodeMapper.selectRdaWfNodeByFromIdList(nodeId,operationType);
    }

    @Override
    public int updateRdaWfNodeByStartTime(RdaWfNode rdaWfNode) {
        return rdaWfNodeMapper.updateRdaWfNodeByStartTime(rdaWfNode);
    }

    @Override
    public int updateRdaWfNodeByNodeIdEnd(Long workflowid, Long stateid, String responseType, Date startTime) {
        return rdaWfNodeMapper.updateRdaWfNodeByNodeIdEnd(workflowid,stateid,responseType,startTime);
    }

    @Override
    public int updateRdaWfNodeByRejectskipcomplete(Long id, boolean Rejectskipcomplete) {
        return rdaWfNodeMapper.updateRdaWfNodeByRejectskipcomplete(id,Rejectskipcomplete);
    }
}
